#include <jni.h>
#include <string.h>
#include <string>
#include <Hilog/log.h>

extern "C" {
#include "libtest.h"
}

//java调用so方法
extern "C"
JNIEXPORT jint JNICALL
Java_com_wex_jni_JniTest_testjniFun(JNIEnv* env, jobject obj, jint a, jint b) {
    return fun(a, b);
}

//字符串转换测试
extern "C"
JNIEXPORT jstring JNICALL
Java_com_wex_jni_JniTest_testjniHello(JNIEnv* env, jobject obj) {
    std::string hello = "Hello from JNI C++ codes";
    int len = hello.size();
    jchar res[len];
    for (int i = 0; i < len; i++)
        res[i] = (jchar)hello[i];
    return env->NewString(res, len);
}

// --- java调用c++触发c++调用java方法 ---

//构建c++测试类
class NativeTest {
public:
    //获取静态句柄
    static NativeTest* GetContent() {
        static NativeTest *content = nullptr;
        if (!content)
            content = new NativeTest();
        return content;
    }
    //设置传入java类的虚拟环境句柄,以及将被回调的函数名
    void SetJvm(JavaVM *jvm, jobject obj, jstring funName)
    {
        this->jvm = jvm;
        this->obj = obj;
        this->funName = funName;
    }
    //c++通过java虚拟环境句柄,查询定位到java方法,并实现调用
    void CallJavaFun(jstring funName) {
        ;
    }
private:
    JavaVM *jvm = nullptr;
    jobject obj = 0;
    jstring funName = nullptr;
};

extern "C"
JNIEXPORT jint JNICALL
Java_com_wex_jni_JniTest_nativeTest(JNIEnv* env, jobject obj, jstring funName) {
    return 0;
}
